#!/bin/bash

set -e

REV=$(cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//);
REV_PARTS=(${REV//\./ });
REV=${REV_PARTS[0]};
if ! [[ "$REV" =~ ^[0-9]+$ ]]; then
    REV=7;
fi

echo "configure redis..."
if [ -e /etc/redis.conf ]; then
    sed -i "s/bind .*/bind 127.0.0.1/g" /etc/redis.conf
    sed -r "/^save\s[0-9]+/d" -i /etc/redis.conf
    systemctl restart redis
fi

echo "configure pgsql..."
postgresql-setup initdb || true
sed "/host\s*all\s*all\s*127\.0\.0\.1\/32\s*ident$/s|ident$|trust|" -i /var/lib/pgsql/data/pg_hba.conf
sed "/host\s*all\s*all\s*::1\/128\s*ident$/s|ident$|trust|" -i /var/lib/pgsql/data/pg_hba.conf

echo "startup service..."
services="rabbitmq-server postgresql redis supervisord"
for SVC in $services; do
    systemctl start $SVC
    systemctl enable $SVC
done

echo "configure documentserver..."
declare -x DS_PORT=8083
DS_RABBITMQ_HOST=localhost;
DS_RABBITMQ_USER=guest;
DS_RABBITMQ_PWD=guest;
DS_REDIS_HOST=localhost;
DS_COMMON_NAME=${DS_COMMON_NAME:-"ds"};
DS_DB_HOST=localhost;
DS_DB_NAME=$DS_COMMON_NAME;
DS_DB_USER=$DS_COMMON_NAME;
DS_DB_PWD=$DS_COMMON_NAME;
declare -x JWT_ENABLED=true;
declare -x JWT_SECRET="secret";
declare -x JWT_HEADER="Authorization";
if ! su - postgres -s /bin/bash -c "psql -lqt" | cut -d \| -f 1 | grep -q ${DS_DB_NAME}; then
    su - postgres -s /bin/bash -c "psql -c \"CREATE DATABASE ${DS_DB_NAME};\""
    su - postgres -s /bin/bash -c "psql -c \"CREATE USER ${DS_DB_USER} WITH password '${DS_DB_PWD}';\""
    su - postgres -s /bin/bash -c "psql -c \"GRANT ALL privileges ON DATABASE ${DS_DB_NAME} TO ${DS_DB_USER};\""
fi
expect << EOF
    set timeout -1
    log_user 1
    spawn documentserver-configure.sh
    expect "Configuring database access..."
    expect -re "Host"
    send "\025$DS_DB_HOST\r"
    expect -re "Database name"
    send "\025$DS_DB_NAME\r"
    expect -re "User"
    send "\025$DS_DB_USER\r"
    expect -re "Password"
    send "\025$DS_DB_PWD\r"
    expect "Configuring redis access..."
    send "\025$DS_REDIS_HOST\r"
    expect "Configuring AMQP access... "
    expect -re "Host"
    send "\025$DS_RABBITMQ_HOST\r"
    expect -re "User"
    send "\025$DS_RABBITMQ_USER\r"
    expect -re "Password"
    send "\025$DS_RABBITMQ_PWD\r"
    expect eof
EOF

echo "configure nginx..."
NGINX_ROOT_DIR="/etc/nginx"
if [ "$REV" = "8" ]; then
    cp -f ${NGINX_ROOT_DIR}/includes/${package_sysname}-communityserver-nginx.conf.template ${NGINX_ROOT_DIR}/nginx.conf
fi
NGINX_WORKER_PROCESSES=${NGINX_WORKER_PROCESSES:-$(grep processor /proc/cpuinfo | wc -l)};
NGINX_WORKER_CONNECTIONS=${NGINX_WORKER_CONNECTIONS:-$(ulimit -n)};
sed 's/^worker_processes.*/'"worker_processes ${NGINX_WORKER_PROCESSES};"'/' -i ${NGINX_ROOT_DIR}/nginx.conf
sed 's/worker_connections.*/'"worker_connections ${NGINX_WORKER_CONNECTIONS};"'/' -i ${NGINX_ROOT_DIR}/nginx.conf
# systemctl restart nginx
systemctl enable nginx
nginx || true

echo "configure firewalld..."
if rpm -q firewalld; then
    firewall-cmd --permanent --zone=public --add-service=http || true
    firewall-cmd --permanent --zone=public --add-service=https || true
    systemctl restart firewalld.service || true
fi
